package com.cuotoniu.lazybug.common.util;

import java.io.File;

/**
 * Path is for easy to get any kind path,include 
 * of the class root path and web root path
 * 
 * @author HLJ
 *
 */
public class Path {

	private String webRootPath = "";
	
	private String classRootPath = "";
	
	private static String WEB_ROOT_PATH_MARK = "WEB-INF";
	private static String CLASS_ROOT_PATH_MARK = "classes";
	
	private StringBuffer path_str = new StringBuffer(200);
	
	/**
	private String path="";
	private String filename="";
	*/
	
	/**
	 * constructor
	 * 
	 */
	public Path() {
		init();
	}
	
	/**
	 * get the web root path
	 * return the dir path with the web root
	 * 
	 * @return
	 */
	public String getWebRootPath(){
		if ("".equals(webRootPath)) {
			init();
		}
		return webRootPath;
	}
	
	/**
	 * get the class root path
	 * return the dir path with the class root
	 * 
	 * @return
	 */
	public String getClassRootPath(){
		if ("".equals(classRootPath)) {
			init();
		}
		return classRootPath;
	}
	
	/**
	 * get the class path with the package path
	 * package path description: separate with the '.'
	 * example:com.cuotoniu.lazybug
	 * and the getPathWithPackage will resolve to <class root path>\com\cuotoniu\lazybug
	 * 
	 * @param packagePath
	 * @return
	 */
	public String getPathWithPackage(String packagePath){
		String[] packages = packagePath.split("\\.");
		path_str.setLength(0);
		path_str.append(getClassRootPath());
		for (String s : packages) {
			path_str.append(s).append(File.separator);
		}
		return path_str.toString();
	}
	
	
	/**
	 * get the web path with the suffixPath(ServletPath)
	 * suffixPath description:convert the '/' to '\'.
	 * example: /image/example.jpg.
	 * getPathWithWeb will resolve to <web root path>\image\example.jgp
	 * 
	 * @param suffixPath
	 * @return
	 */
	public String getPathWithWeb(String suffixPath){
		path_str.setLength(0);
		path_str.append(getWebRootPath());
		suffixPath = suffixPath.replaceAll("/", "\\\\");
		if (suffixPath.startsWith("\\")) {
			path_str.append(suffixPath.toString().substring(1));
		}else{
			path_str.append(suffixPath.toString());
		}
		return path_str.toString();
	}
	
	/**
	 * initial the web root path and class root path.
	 * 
	 */
	public void init(){
		
		String path = Path.class.getProtectionDomain().getCodeSource().getLocation().getPath();
		
		String[] paths = path.split("/");
		
		path_str.setLength(0);
		/** int cnt = 0; */
		for (String s : paths) {
			/**	cnt++; */
			if (s != null && !"".equals(s.trim())) {
				if (WEB_ROOT_PATH_MARK.equalsIgnoreCase(s)) {
					webRootPath = path_str.toString();
				}
				if(CLASS_ROOT_PATH_MARK.equalsIgnoreCase(s)){
					path_str.append(s).append(File.separator);
					classRootPath = path_str.toString();
				}
				/**
				if (paths.length == cnt) {
					filename = s;
				}else{
					sb.append(s).append(File.separator);
				}
				*/
				path_str.append(s).append(File.separator);
			}
		}
        /** this.path = sb.toString().substring(0,sb.toString().length()-1); */
	}
	
}
